package cn.bdqn.sys.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
//import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LoginController {
	@RequestMapping("/login")
	public String login() {
		return "login page";
	}

	@RequestMapping("loginAction")
	public String loginAction(String username, String password) {
		// String md5hash = new Md5Hash(password,"123").toString();
		UsernamePasswordToken token = new UsernamePasswordToken(username, /* md5hash */"");
		Subject subject = SecurityUtils.getSubject();
		try {
			// 如果执行subject.login不抛出异常，则证明登录成功。
			subject.login(token);
			return "Success!";
		} catch (Exception e) {
			// 有异常证明登录错误
			e.printStackTrace();
			return "Failed";
		}
	}
}